<?php
namespace Amoysea\Controller;
use Think\Controller;
class BillController extends BaseController {
	
	public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
        C('erp_dbname',$g['db']);
    }
	//绑定
	public function bind_list_bill(){
		$m=M('BindListBill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$b=M('Bill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$l=M('List', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$ids=I('ids');
		$bill_id=I('id');
		$data['bill_id']=$bill_id;
		foreach($ids as $k=>$v){
			$data['list_id']=$v;
			$info=$m->where(array('list_id'=>$v,'bill_id'=>$bill_id))->find();
			if($info){
				continue;
			}else{
				$res=$m->add($data);
				if(!$res){
					$return['status']=0;
					$return['message']="添加失败";
					$this->ajaxReturn($return);
				}else{
					$l->where(array('id'=>$v))->setField('bind_status',1);
					continue;
				}
			}
		}
		$b->where(array('id'=>$bill_id))->setField('bind_status',1);
		$return['status']=1;
		$return['message']="添加成功";
		$this->ajaxReturn($return);
	}
	//绑定采购单
	public function bind_list(){
		$List=M('List', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$m=M('BindListBill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$id=I('id');
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['order_no'] = array('like','%'.$keywords.'%');
			}
        }
		// $map['is_examined']=4;
		$map['bind_status']=0;
		$count=$List->where($map)->count();
		$list=$List->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
// dump($list);
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$bind_bill_list=$m->alias('m')->field('l.*')->join('__LIST__ l on m.list_id=l.id')->where(array('bill_id'=>$id))->select();
		
        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign("bind_bill_list",$bind_bill_list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();
	}
    //部门列表
	public function bill(){
		$Bill=M('Bill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$BillCate=M('BillCate', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        $ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$Bill->where($map)->count();
		$list=$Bill->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$info_cate=$BillCate->where(array('no'=>$v['bill_cate']))->find();
			$info_company=$ContactsCompany->where(array('no'=>$v['company_no']))->find();
			$list[$k]['billcate_name']=$info_cate['name'];
			$list[$k]['company_name']=$info_company['name'];
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	
	//添加、更新
	function add_bill(){
		$BillCate=M('BillCate', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$billcate_list=$BillCate->select();
		$company_list=$ContactsCompany->select();
		$this->assign('billcate_list',$billcate_list);
		$this->assign('company_list',$company_list);
		if(IS_POST){
			$Bill=M('Bill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$param['create_bill_time']=strtotime($param['create_bill_time']);
			$id=$param['id'];
			if($id){	//更新
				$param['update_time']=time();
				$this->validate_bill($param,0);//验证输入字符，参数2为0时表示更新
				$res=$Bill->save($param);
				// echo $Bill->getlastsql();
				// die;
				if($res!==false){
					$this->success('更新成功');
				}
			}else{	//添加
				$param['add_time']=time();
				$this->validate_bill($param,1);//验证输入字符,参数2为1时表示添加
				// $data['no']=1;
				$res=$Bill->add($param);
				if($res){
					$this->success('添加成功');
				}
			}
		}else{
			$this->display();
		}
	}
	
	//编辑发票类型
	function edit_bill(){
		$id=trim(I('id'));
		$Bill=M('Bill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$Bill->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		
		$BillCate=M('BillCate', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$billcate_list=$BillCate->select();
		$company_list=$ContactsCompany->select();
		$this->assign('billcate_list',$billcate_list);
		$this->assign('company_list',$company_list);
		$this->display();
	}
	
	//删除发票类型
	function del_bill(){
		$id=trim(I('id'));
		$Bill=M('Bill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		
		$status=$Bill->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	
	//验证输入字符
	function validate_bill($param,$status=1){ //status为1表示添加操作，为0表示更新操作
			$Bill=M('Bill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			if($param['no']==''){
				$this->error('代码不能为空！');
			}
			if($param['tax_no']==''){
				$this->error('税号不能为空！');
			}
		
			if($status){ //添加重复判断
				$bill_no=$Bill->where(array('no'=>$param['no']))->find();
				$bill_name=$Bill->where(array('tax_no'=>$param['tax_no']))->find();
			}else{  //更新重复判断
				$bill_no=$Bill->where(array('no'=>$param['no'],'id'=>array('neq',$param['id'])))->find();
				$bill_name=$Bill->where(array('tax_no'=>$param['tax_no'],'id'=>array('neq',$param['id'])))->find();
			}
			
			if($bill_no){
				$this->error('发票代码重复！');
			}
			if($bill_name){
				$this->error('发票税号重复！');
			}
	}
	
}